//
//  AppInfoModel.m
//  gdemm
//
//  Created by 李园园 on 2017/8/14.
//  Copyright © 2017年 Goldencis. All rights reserved.
//

#import "AppInfoModel.h"
#import "getAppInfo.h"
#import "PackageInfo.h"
@interface AppInfoModel ()
@property(nonatomic,strong)NSMutableArray * installedAppInfoArray;
@end

@implementation AppInfoModel

-(NSMutableArray *)installedAppInfoArray{
    if (_installedAppInfoArray == nil) {
        _installedAppInfoArray = [getAppInfo getAllAppInfo];
    }
    return _installedAppInfoArray;
}
- (instancetype)initWithDictionary:(NSDictionary *)dict{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
         self.appState = @"安装";
        self.size = [self getAppSize:dict[@"size"]];
        for (PackageInfo * info in self.installedAppInfoArray) {
            if ([info.packageName isEqualToString:dict[@"packageName"]]) {
                if ([info.version isEqualToString:dict[@"version"]]) {
                    self.appState = @"打开";
                }else{
                    NSArray * localAppVersionArray = [info.version componentsSeparatedByString:@"."];
                    NSArray * installAppVersionArray = [dict[@"version"] componentsSeparatedByString:@"."];
                    BOOL isUpdate =  [self isUpdate:localAppVersionArray other:installAppVersionArray];
                    if (isUpdate) {
                         self.appState = @"更新";
                    }
                }
                break;
            }
        }
        if ([self.appState isEqualToString:@"安装"]) {
          
        }
        
    }
    return self;
}

-(BOOL)isUpdate:(NSArray *)localAppVersionArray other:(NSArray *)installAppVersionArray{
    if (localAppVersionArray.count < installAppVersionArray.count) {
        for (int i = 0; i< localAppVersionArray.count; i++) {
            if([localAppVersionArray[i] integerValue] < [installAppVersionArray[i] integerValue]){
                return YES;
            }else if ([localAppVersionArray[i] integerValue] > [installAppVersionArray[i] integerValue] ){
                return NO;
            }
        }
    }else{
        for (int i = 0; i< installAppVersionArray.count; i++) {
            if([localAppVersionArray[i] integerValue] < [installAppVersionArray[i] integerValue]){
                return YES;
            }else if ([localAppVersionArray[i] integerValue] > [installAppVersionArray[i] integerValue] ){
                return NO;
            }
        }
    }
    return YES;
}
//计算包的大小
-(NSString *)getAppSize:(NSString *)appSize{
      unsigned long long size = [appSize intValue];
        unsigned long long length = size / 1024;
        if (length < 1) {
            return @"1MB";
        }
        if (length >= 1 && length < 1000) {
            return @"1MB";
        }
        length = length / 1024;
        if (length >= 1) {
            return [NSString stringWithFormat:@"%lldMB",length];
        }
     return @"1MB";
    
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}
+ (instancetype)appInfoWithDictionary:(NSDictionary *)dict{
    
    return [[AppInfoModel alloc]initWithDictionary:dict];
}
+ (NSArray *)appInfoArrayWithArray:(NSArray *)array{
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        if (![dict[@"packageName"] isEqualToString:@"cn.neiwang.emm"]) {
            [arr addObject:[AppInfoModel appInfoWithDictionary:dict]];
        }
    }
    return arr;
}
@end
